package com.kdtech.analyse.video;
import java.util.HashMap;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

public class ChinanewsVideoAnalyse implements AnalyseNews{



	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://www.chinanews.com/.*shipin/.*[0-9]+/[0-9]+-[0-9]+/news[0-9]+.shtml",
				};
		
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		video.setUrl(url);		
		String title = null;
		String desc = null;
		Long date = null;
		Document doc = Jsoup.parse(htmltxt);
		
		title = doc.select("div.video_con h1").text();
		
		desc = doc.select("meta[name=description]").attr("content");
		
		date = DateUtils.matchDate(doc.select("p.Submit_time").text());
		
		String author = doc.select("p.Submit_time span:containsOwn(来源：").text();
		if(StringUtils.isNotBlank(author)){
			author = StringUtils.substringAfter(author, "来源：");
		}
		video.setTitle(StringUtils.trimSpace(title));
		video.setContent(desc);
		video.setAuthor(author);
		video.setDate(date);
		video.setUpdateUrl(url);
		return video;
	}
	

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	

	public static HashMap<String,String>  getLinks(String response) {
		// TODO Auto-generated method stub
		try {
			response = StringUtils.substringAfter(response, "=");
			JSONObject json = JSONObject.parseObject(response);
			JSONArray array = json.getJSONArray("videos");
			HashMap<String,String> links = new HashMap<String, String>();
			for(int i=0;i<array.size();i++){
				String url = array.getJSONObject(i).getString("url");
				links.put(url, null);
			}
			return links;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

}
